<?php

namespace Database\Seeders;

use App\Models\Data\ClickLearn;
use App\Models\Data\ClickLearnSpell;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class ClickLearnDataSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        $click_learns=DB::connection('rjmysql')->table('click_learns')->get()->toArray();
        $click_learns=json_decode(json_encode($click_learns),true);
        foreach ($click_learns as $click_learn) {
            ClickLearn::query()->updateOrCreate(
                ['id' => $click_learn['id']],
                $click_learn
            );
            if (!ClickLearnSpell::query()->where('click_learn_id',$click_learn['id'])->exists()) {
                $this->save_click_learn_spell($click_learn['id']);
            }
        }
        $this->command->info('内容数据填充完成！');
    }
    protected function save_click_learn_spell(int $click_learn_id): void
    {
        $click_learn_spells=DB::connection('rjmysql')->table('click_learn_spells')->where('click_learn_id',$click_learn_id)->get()->toArray();
        $click_learn_spells=json_decode(json_encode($click_learn_spells),true);
        foreach ($click_learn_spells as $click_learn_spell) {
            ClickLearnSpell::query()->updateOrCreate(
                ['id' => $click_learn_spell['id']],
                $click_learn_spell
            );
        }
    }
}
